﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Program_ApplicationData
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class MainPage : Program_ApplicationData.Common.LayoutAwarePage
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            
        }
        
        Windows.Storage.ApplicationDataContainer localSettings;
        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SaveSettingAsync("setting1",inputText.Text, localSettings);
            SaveSettingCompositeAsync("setting2",inputText2.Text, localSettings);
            SaveSettingViaContainerAsync("setting1",inputText3.Text, "container", localSettings);
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            outputText2.Text = await LoadSettingAsync("setting1", localSettings);
            ApplicationDataCompositeValue compositeValue = await LoadSettingCompositeAsync("setting2", localSettings);
            outputText3.Text = compositeValue["strVal"].ToString();
            outputText4.Text = await LoadSettingViaContainerAsync("setting1","container", localSettings);
        }

      

        private async void SaveSettingAsync(string settingKey,string settingValue,ApplicationDataContainer settings)
        {
            settings.Values[settingKey] = settingValue;
        }

        private async Task<String> LoadSettingAsync(string settingKey, ApplicationDataContainer settings)
        {
            return settings.Values[settingKey].ToString();
        }

        private async void SaveSettingCompositeAsync(string settingKey,string settingValue,ApplicationDataContainer settings)
        {
            ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
            composite["intVal"] = 1;
            composite["strVal"] = settingValue;

            settings.Values[settingKey] = composite;
        }


        private async Task<ApplicationDataCompositeValue> LoadSettingCompositeAsync(string settingKey, ApplicationDataContainer settings)
        {

            ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)settings.Values[settingKey];
            if (composite == null)
                return null;
            else
                return composite;

        }

        private async void SaveSettingViaContainerAsync(string settingKey,string settingValue, string settingContainer,ApplicationDataContainer settings)
        {
            ApplicationDataContainer container = settings.CreateContainer(settingContainer, ApplicationDataCreateDisposition.Always);

            if (settings.Containers.ContainsKey(settingContainer))
            {
                settings.Containers[settingContainer].Values[settingKey] = settingValue;
            }
        }

        private async Task<String> LoadSettingViaContainerAsync(string settingKey, string settingContainer, ApplicationDataContainer settings)
        {
            bool hasContainer = settings.Containers.ContainsKey(settingContainer);
            if (hasContainer)
            {
                return settings.Containers[settingContainer].Values[settingKey].ToString();
            }
            else
                return null;
        }



    }
}
